package com.loutick.pay.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sun.util.logging.resources.logging;

import com.loutick.basicforapp.entity.TUser;
import com.loutick.pay.controller.PayController;
import com.loutick.pay.dao.TOrderMapper;
import com.loutick.pay.dao.TPamentlogMapper;
import com.loutick.pay.dao.TUseraccountMapper;
import com.loutick.pay.dao.TUseraccountlogMapper;
import com.loutick.pay.entity.TOrder;
import com.loutick.pay.entity.TPamentlog;
import com.loutick.pay.entity.TUseraccount;
import com.loutick.pay.entity.TUseraccountlog;
import com.loutick.pay.service.IPamentlog;

@Service("pamentlogService")   
@Transactional
public class PamentlogServiceImpl implements IPamentlog {  
	private static Logger logger = Logger.getLogger(PamentlogServiceImpl.class);
	@Resource
	private TPamentlogMapper pamentlogMapper ;
	
	@Resource
	private TUseraccountMapper useraccountMapper;
	
	@Resource
	private TUseraccountlogMapper useraccountlogMapper;
	
	@Resource
	private TOrderMapper orderMapper;
	
	 
	public int insertPamentlog(TPamentlog record,Integer userid) {
//		logger.error("select tUseraccount"  );
		TUseraccount tUseraccount = useraccountMapper.selectByUserid(userid);
//		logger.error("tUseraccount"  );
		
		if(tUseraccount == null) {
			TUseraccount tu = new TUseraccount();
			tu.setUserid(userid);
			int res = useraccountMapper.insertSelective(tu);
//			logger.error("res："+res  );
		}
		else{
			record.setUseraccountid(tUseraccount.getUseraccountid());
		}
		return pamentlogMapper.insertSelective(record) ;
	}

	public TPamentlog selectByOuttradesn(String outtradesn) {
		
		return pamentlogMapper.selectByOuttradesn(outtradesn);
	}
	
	public int insertOrder(TOrder order){
		
		return orderMapper.insertSelective(order);
	}
	
	public TOrder selectByInfo(TOrder order) {
		
		return orderMapper.selectByInfo(order);
	}
	
	public String rechargeLog(String outtradesn,String tradeno){ 
		logger.error("充值记录  outtradesn ："+outtradesn);
		logger.error("充值记录  tradeno ："+tradeno);
		//查找pamentlog中的充值日志是否存在；
		TPamentlog tp = pamentlogMapper.selectByOuttradesn(outtradesn);
		if(tp == null) { logger.error("充值记录不存在 outtradesn ："+outtradesn);  return "无支付日志";}
		
		//修改pamentlog信息
		tp.setTradeno(tradeno);
		tp.setPaytime(new Date());
		tp.setPaystatus(1);
		int res = pamentlogMapper.updateByPrimaryKeySelective(tp);
		logger.error("充值记录  res ："+res);
		
		
		String chargetype = "" ;
		chargetype = tp.getPaytotype();
		if(chargetype.equals("20")){
		
			Integer useraccountid = tp.getUseraccountid();
			//向对应用户账户充值；
			TUseraccount tUseraccount = useraccountMapper.selectByPrimaryKey(useraccountid);
			Integer newAmount = tUseraccount.getAmount()+tp.getPayamount();
			Integer oldAmount = tUseraccount.getAmount();
			tUseraccount.setAmount(newAmount);
			useraccountMapper.updateByPrimaryKeySelective(tUseraccount);
			//记录用户账户充值记录
			TUseraccountlog useraccountlog = new TUseraccountlog();
			useraccountlog.setAmount(tp.getPayamount());
			useraccountlog.setCurrentamount(oldAmount);
			useraccountlog.setChargetype("I01");
			useraccountlog.setLogtime(new Date());
			useraccountlog.setPaymentlogid(tp.getPaymentlogid());
			useraccountlog.setUseraccountid(useraccountid);
			useraccountlogMapper.insertSelective(useraccountlog);
			return "充值成功";
		}else if(chargetype.equals("10")){
			return "支付成功" ;
		}
		
		return "充值成功" ;
	}

	

}
